package com.android.delivery.activity.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.android.delivery.App;
import com.android.delivery.R;
import com.android.delivery.activity.CapitalListActivity;
import com.android.delivery.activity.ContactUsActivity;
import com.android.delivery.activity.LoginActivity;
import com.android.delivery.activity.PersonActivity;
import com.android.delivery.activity.PrintlnActivity;
import com.android.delivery.activity.WebViewActivity;
import com.android.delivery.api.ApiService;
import com.android.delivery.base.AppUtils;
import com.android.delivery.base.LazyFragment;
import com.android.delivery.callback.DialogCallback;
import com.android.delivery.callback.LzyResponse;
import com.android.delivery.constant.Constants;
import com.android.delivery.entity.LoginInfo;
import com.android.delivery.entity.User;
import com.android.delivery.entity.even.RefreshUserEvent;
import com.android.delivery.util.GlideUtil;
import com.android.delivery.util.StringUtils;
import com.blankj.utilcode.util.ActivityUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lzy.okgo.model.Response;
import com.wuhenzhizao.sku.utils.ScreenUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MyFragment extends LazyFragment {
    private ImageView iv_photo;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_money;
    private TextView tv_print;
    private View tv_print_line;
    private TextView tv_version;
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_my, container, false);
//        return view;
//    }

    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        return view;
    }

    @Override
    protected void initData() {

    }

    void getInfo() {
        ApiService.user_info(this, new DialogCallback<LzyResponse<User>>() {
            @Override
            public void onSuccess(Response<LzyResponse<User>> response) {
                User user = response.body().data;
                if (user != null){
                    GlideUtil.loadCircleImg(iv_photo, user.getAvatar());
                    tv_name.setText(user.getNickname());
                    tv_phone.setText(StringUtils.getFormatPhone(user.getMobile()));
                }
            }
        });
    }

    private void getBalance() {
        ApiService.getUserBalance(this, new DialogCallback<LzyResponse<User>>() {
            @Override
            public void onSuccess(Response<LzyResponse<User>> response) {
                User money = response.body().data;
                if (!TextUtils.isEmpty(money.getBalance())){
                    if (tv_money != null){
                        tv_money.setText(money.getBalance());
                    }
                }
            }
        });
    }

    @Override
    public void lazyInit() {
        iv_photo = getView().findViewById(R.id.iv_photo);
        tv_name = getView().findViewById(R.id.tv_name);
        tv_phone = getView().findViewById(R.id.tv_phone);
        tv_money = getView().findViewById(R.id.tv_money);
        tv_print = getView().findViewById(R.id.tv_print);
        tv_print_line = getView().findViewById(R.id.line3);
        tv_version = getView().findViewById(R.id.tv_version);

        ConstraintLayout infoLayout = getView().findViewById(R.id.layout_info);
        infoLayout.setPadding(0, ScreenUtils.getStatusBarHeight(getActivity()), 0, 0);

        infoLayout.setOnClickListener(v -> {
            ActivityUtils.startActivity(PersonActivity.class);
        });

        getView().findViewById(R.id.tv_person).setOnClickListener(v -> {
            ActivityUtils.startActivity(PersonActivity.class);
        });

        getView().findViewById(R.id.tv_zjmx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startActivity(CapitalListActivity.class);
            }
        });

        getView().findViewById(R.id.tv_contact).setOnClickListener(v -> {
            ActivityUtils.startActivity(ContactUsActivity.class);
        });

        tv_print.setOnClickListener(v -> {
            ActivityUtils.startActivity(PrintlnActivity.class);
        });


        getView().findViewById(R.id.tv_logout).setOnClickListener(v -> {
            new XPopup.Builder(getContext())
//                         .dismissOnTouchOutside(false)
//                         .autoDismiss(false)
//                        .popupAnimation(PopupAnimation.NoAnimation)
                    .asConfirm("", "是否确认退出登录？",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @Override
                                public void onConfirm() {
                                    CloudPushService mPushService = PushServiceFactory.getCloudPushService();
                                    mPushService.unbindAccount(new CommonCallback() {
                                        @Override
                                        public void onSuccess(String s) {

                                        }

                                        @Override
                                        public void onFailed(String s, String s1) {

                                        }
                                    });
                                    App.Companion.getInstance().clearLoginInfo();
                                    LoginActivity.self(getActivity());
                                }
                            }, null, false)
                    .show();
        });

        getView().findViewById(R.id.tv_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(getActivity(), "服务协议", Constants.SERVICE_PROTOCOL);
            }
        });

        getView().findViewById(R.id.tv_protocol).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(getActivity(), "隐私政策", Constants.PRIVATE_PROTOCOL);
            }
        });

        LoginInfo loginInfo = App.Companion.getInstance().getLoginInfo();
        if (loginInfo != null) {
            //角色，1：网点，2：骑手，3：司机
            if (loginInfo.getRole() == 1 || loginInfo.getRole() == 2){
                tv_print.setVisibility(View.VISIBLE);
                tv_print_line.setVisibility(View.VISIBLE);
            }else {
                tv_print.setVisibility(View.GONE);
                tv_print_line.setVisibility(View.GONE);
            }
        }

        tv_version.setText("V"+AppUtils.getVersionName(getActivity()));

        getInfo();
        getBalance();
    }

    @Override
    public void onResume() {
        super.onResume();
        getBalance();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void addrEven(RefreshUserEvent refreshUserEvent) {
        getInfo();
    }

    @Override
    protected boolean isBindEventBus() {
        return true;
    }
}
